package com.example.zhaojianxu.signin.psychological.test

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioGroup
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.common.bean.BaseFragment
import com.example.zhaojianxu.signin.psychological.http.PsycEntity
import com.example.zhaojianxu.signin.psychological.http.PsycItemEntity
import kotlinx.android.synthetic.main.fragment_test.*

/**
 * create by @zhaojianxu on 2019/2/15
 */
class TestFragment : BaseFragment() {

    var mView: View? = null
    var mListener: IPTestListener? = null
    var mPosition = 0
    var mResult = 0
    private var model: PsycEntity? = null


    override fun initView() {
        val radioGroup = mView!!.findViewById<RadioGroup>(R.id.fragment_test_radio_group)
        radioGroup!!.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.fragment_test_radio_1 -> {
                    model?.result = model?.items?.get(0)?.itemResult ?: 1
                }
                R.id.fragment_test_radio_2 -> {
                    model?.result = model?.items?.get(1)?.itemResult ?: 1
                }
                R.id.fragment_test_radio_3 -> {
                    model?.result = model?.items?.get(2)?.itemResult ?: 1
                }
                R.id.fragment_test_radio_4 -> {
                    model?.result = model?.items?.get(3)?.itemResult ?: 1
                }
            }
            mListener?.success(mPosition, model?.result ?: 1)
        }
    }

    override fun initData() {
        mPosition = arguments?.getInt("position") ?: 0
        model = arguments?.getSerializable("model") as? PsycEntity
        mListener = activity as? IPTestListener

        Log.d("TAGGGGG","position = $mPosition   model = $model")

        fragment_test_number.text = "第${mPosition+1}题："
        fragment_test_content.text = model?.content

        fragment_test_radio_1.text = model?.items?.get(0)?.option
        fragment_test_radio_2.text = model?.items?.get(1)?.option
        fragment_test_radio_3.text = model?.items?.get(2)?.option
        fragment_test_radio_4.text = model?.items?.get(3)?.option

    }

    companion object {
        fun getInstance(position: Int, model: PsycEntity): TestFragment{

            return TestFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mView = inflater.inflate(R.layout.fragment_test,container,false)
        return mView
    }
}